#Filename: chinapub.py 
from Tkinter import *
import ImageTk
import vcode
from urllib2 import urlopen
from os.path import exists

class Chinapub(Frame):
        def getBmp(self):
                vcodebmp = urlopen(self.url).read()

                file = open(self.codebmp, 'rb')
                file.write(vcodebmp)
                file.close()

                self.displayimg = ImageTk.PhotoImage(file=self.codebmp)
                self.BMP['image'] = self.displayimg
                self.BMP.pack()

        def showCode(self):
                if exists(self.codebmp):
                        result = vcode.getCode(self.codebmp)
                        self.CODE['text'] = result
                else:
                        pass

        def createWidgets(self):
                self.URL = Label(self)
                self.url = 'http://tool.chinaz.com/beian.aspx?at=img'
                self.URL['text'] = self.url
                self.URL['fg'] = 'red'

                self.URL.pack({"side": "top"})

                self.BMP = Label()
                self.img = ImageTk.PhotoImage(file='D://python//capt//beian.bmp')
                self.BMP['image'] = self.img

                self.BMP.pack()

                self.CODE = Label()
                self.CODE['text'] = 'python'

                self.CODE.pack()

                self.bottom = Label(self)
                self.bottom.pack({"side": "bottom"})

                self.GETBMP = Button(self.bottom)
                self.GETBMP['text'] = 'Get bmp'
                self.GETBMP['command'] = self.getBmp

                self.GETBMP.pack({"side": "left"})

                self.GETCODE = Button(self.bottom)
                self.GETCODE['text'] = 'Get code'
                self.GETCODE['command'] = self.showCode

                self.GETCODE.pack({"side": "right"})

        def __init__(self, master=None):
                Frame.__init__(self, master)
                self.codebmp = 'D://python//capt//code.bmp'
                self.pack()
                self.createWidgets()


root = Tk()
app = Chinapub(master=root)
app.mainloop()
